﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FAMailUtils.MailGun
{
    public class BounceTrackerItem
    {
        public string created_at { get; set; }
        public int code { get; set; }
        public string address { get; set; }
        public string error { get; set; }
    }
    public class AddBounceResult
    {
        public bool isAdded { get; set; }
        public string message { get; set; }
        public string address { get; set; }
    }
    public class BounceTrackerResult
    {
        public bool isGetOK { get; set; }
        public string message { get; set; }
        public decimal total_count { get; set; }
        public List<BounceTrackerItem> items { get; set; }
    }
    public class BounceTracker
    {
        string ApiKey;
        public BounceTracker(string ApiKey)
        {
            this.ApiKey = ApiKey;
        }
        /// <summary>
        /// Fetch the full list of all recipient addresses that bounced
        /// </summary>
        /// <param name="domainName"></param>
        /// <returns></returns>
        public BounceTrackerResult GetBounces(string domainName)
        {
            BounceTrackerResult result = new BounceTrackerResult();
            try
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.mailgun.net/v2";
                client.Authenticator =
                        new HttpBasicAuthenticator("api",
                                                   ApiKey);
                RestRequest request = new RestRequest();
                request.AddParameter("domain",
                                     domainName, ParameterType.UrlSegment);
                request.Resource = "{domain}/bounces";
                IRestResponse response = client.Execute(request);

                result = Newtonsoft.Json.JsonConvert.DeserializeObject<BounceTrackerResult>(response.Content);
                result.isGetOK = true;
                return result;
            }
            catch (Exception ex) { result.isGetOK = false; result.message = ex.Message; } return result;
        }
        /// <summary>
        /// Lets check if any messages sent to Address have bounced before
        /// </summary>
        /// <param name="domainName"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public string GetBounce(string domainName, string address)
        {
            try
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.mailgun.net/v2";
                client.Authenticator =
                        new HttpBasicAuthenticator("api",
                                                   ApiKey);
                RestRequest request = new RestRequest();
                request.AddParameter("domain",
                                     domainName, ParameterType.UrlSegment);
                request.Resource = "{domain}/bounces/" + address;
                IRestResponse response = client.Execute(request);
                return response.Content;
            }
            catch (Exception ex) { return ex.Message; }
        }
        /// <summary>
        /// Add a bounce to the table
        /// </summary>
        /// <param name="domainName"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public AddBounceResult AddBounce(string domainName, string address)
        {
            AddBounceResult result = new AddBounceResult();
            try
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.mailgun.net/v2";
                client.Authenticator =
                        new HttpBasicAuthenticator("api",
                                                   ApiKey);
                RestRequest request = new RestRequest();
                request.Resource = "{domain}/bounces";
                request.AddParameter("domain",
                                     domainName, ParameterType.UrlSegment);
                request.AddParameter("address", address);
                request.Method = Method.POST;
                IRestResponse response = client.Execute(request);

                result = Newtonsoft.Json.JsonConvert.DeserializeObject<AddBounceResult>(response.Content);
                result.isAdded = true;
            }
            catch (Exception ex) { result.isAdded = false; result.message = ex.Message; }
            return result;
        }
    }
}
